package com.zhkc.wisdom.auth.filter;

import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Slf4j
public class ForwardAuthFilter implements WebFilter {


    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
        log.info("---------- 进入Sa-Token全局鉴权 -----------");
        ServerHttpRequest serverHttpRequest = serverWebExchange.getRequest();

        String path = serverHttpRequest.getPath().toString();
        //判断用户是否有该权限
        if (!StpUtil.hasPermission(path)) {
            throw new NotPermissionException(path);
        }
        log.info("---------- Sa-Token全局鉴权结束 -----------");
        return webFilterChain.filter(serverWebExchange);
    }

}